<template>
  <div class="xjh-form">
    <div class="header">
      <slot name="header"></slot>
    </div>
    <el-form :label-width="labelWidth" v-model="formData">
      <el-row>
        <template v-for="item in formItems" :key="item.label">
          <el-col v-bind="colLayout">
            <!-- 需要一个input -->
            <el-form-item
              :label="item.label"
              :rules="item.rules"
              :style="itemStyle"
              v-if="!item.isHidden"
              class="form-item"
            >
              <template
                v-if="item.type === 'input' || item.type === 'password'"
              >
                <el-input
                  v-bind="item.otherOptions"
                  :placeholder="item.placeholder"
                  :show-password="item.type === 'password'"
                  v-model="formData[`${item.filed}`]"
                ></el-input>
              </template>
              <!-- 需要一个select -->
              <template v-else-if="item.type === 'select'">
                <el-select
                  :placeholder="item.placeholder"
                  v-bind="item.otherOptions"
                  style="width: 100%"
                  v-model="formData[`${item.filed}`]"
                >
                  <el-option
                    v-for="option in item.options"
                    :key="option.value"
                    :value="option.value"
                    >{{ option.title }}</el-option
                  >
                </el-select>
              </template>
              <!-- 需要一个datepicker -->
              <template v-else-if="item.type === 'datepicker'">
                <el-date-picker
                  style="width: 100%"
                  v-model="formData[`${item.filed}`]"
                  v-bind="item.otherOptions"
                ></el-date-picker>
              </template>
            </el-form-item>
          </el-col>
        </template>
      </el-row>
    </el-form>
    <div class="footer">
      <slot name="footer"></slot>
    </div>
  </div>
</template>

<script setup lang="ts">
import { defineProps, PropType, ref, watch, defineEmits } from 'vue';
import { IFormItem } from '..';

const props = defineProps({
  modelValue: {
    type: Object,
    required: true
  },
  formItems: {
    type: Array as PropType<IFormItem[]>,
    default: () => []
  },
  labelWidth: {
    type: String,
    default: '100px'
  },
  itemStyle: {
    type: Object,
    default: () => ({ padding: '10px 20px' })
  },
  colLayout: {
    type: Object,
    default: () => ({
      xl: 6,
      lg: 8,
      md: 12,
      sm: 24,
      xs: 24
    })
  }
});

// 赋值一份上面传下来的数据,然后通过watch => 更新我的组件传下来的v-model的数据
const formData = ref({ ...props.modelValue });

const emit = defineEmits(['update:modelValue']);

/**这样的watch的处理方式还是很不错的。。 */
// watch(
//   () => props.modelValue,
//   (newVal) => {
//     formData.value = { ...newVal };
//   },
//   {
//     deep: true
//   }
// );

watch(
  () => formData.value,
  (newValue) => {
    // console.log(newValue);
    emit('update:modelValue', newValue);
  },
  {
    deep: true,
    immediate: true
  }
);

// watch(
//   () => props.formItems,
//   (newValue) => {
//     console.log(newValue);
//   },
//   {
//     deep: true,
//     immediate: true
//   }
// );
</script>

<style lang="less" scoped>
.xjh-form {
  padding-top: 22px;
}
</style>
